﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.UserControls;

    public class SKUList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnFinish;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F2062;
        protected GridView gvSku;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlInputHidden hiddenFlag;
        protected Label lblMsg;
        protected Label lblSku;
        protected LinkButton lnkbtnAlternateSkuImportData;
        protected LinkButton lnkbtnImportData;
        protected LinkButton lnkbtnSetActiveFalse;
        protected LinkButton lnkbtnSetActiveTrue;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnAlternateSkuImportData;
        protected HtmlTableCell tdlnkbtnImportData;
        protected HtmlTableCell tdlnkbtnSetActiveFalse;
        protected HtmlTableCell tdlnkbtnSetActiveTrue;

        private void BindData2SkuList(SkuQueryEntity skuQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(skuQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoDetailServiceClientProxy.GetInstance().GetDetaiInfolByQueryEntity(skuQuery, "SKU");
            base.BindData2GridView(this.gvSku, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.Session["SkuList"] = ds;
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> ownerID = base.GetSelectedIDsInGridView(this.gvSku, "OWNER_ID");
                List<string> skuID = base.GetSelectedIDsInGridView(this.gvSku, "SKU_ID");
                if ((skuID == null) || (skuID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2110_016") + BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(ownerID, skuID, new List<string>(), "SKU") + base.GetGlobalMsgResource("C006");
                    SkuQueryEntity skuQuery = this.GetSkuQueryEntity();
                    this.BindData2SkuList(skuQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> ownerID = base.GetSelectedIDsInGridView(this.gvSku, "OWNER_ID");
            List<string> skuID = base.GetSelectedIDsInGridView(this.gvSku, "SKU_ID");
            if ((skuID == null) || (skuID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (skuID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("Sku.aspx?from=list&ID={0}&ID2={1}", base.UrlEncode(ownerID[0]), base.UrlEncode(skuID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            SkuQueryEntity skuQuery = this.GetSkuQueryEntity();
            skuQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = skuQuery.CurrentPage;
            skuQuery.CurrentPage = "0";
            this.gvSku.Columns[1].Visible = false;
            this.gvSku.AllowSorting = false;
            this.BindData2SkuList(skuQuery);
            this.gvSku.ShowFooter = false;
            skuQuery.IsGetAll = false;
            skuQuery.CurrentPage = currentPageIndex;
            this.SetSkuQueryEntity(skuQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("Sku.xls", this.gvSku);
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvSku, "SKU_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else
            {
                for (int i = 0; selectedID.Count > i; i++)
                {
                    this.hdnRtnValue.Value = this.hdnRtnValue.Value + selectedID[i] + ",";
                }
                if (this.hdnRtnValue.Value.Length > 1)
                {
                    this.hdnRtnValue.Value = this.hdnRtnValue.Value.Substring(0, this.hdnRtnValue.Value.Length - 1);
                }
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            SkuQueryEntity skuQuery = this.GetSkuQueryEntity();
            skuQuery.CurrentPage = "0";
            this.BindData2SkuList(skuQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            SkuQueryEntity skuQuery = this.GetSkuQueryEntity();
            skuQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2SkuList(skuQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            SkuQueryEntity skuQuery = this.GetSkuQueryEntity();
            skuQuery.SkuID = "";
            skuQuery.OwnerID = "";
            skuQuery.Descr = "";
            skuQuery.IsActive = "";
            skuQuery.PackID = "";
            skuQuery.LotTemplateID = "";
            skuQuery.SkuClassID = "";
            this.BindData2SkuList(skuQuery);
        }

        protected void btnNewSearch_Click(object sender, EventArgs e)
        {
            string ckb = string.Empty;
            if (!string.IsNullOrEmpty(base.Request.QueryString["ckb"]))
            {
                ckb = "N";
            }
            base.Server.Transfer(string.Format("SkuQuery.aspx?action={0}&ownerID={1}&ckb={2}&OnLine={3}", new object[] { base.UrlEncode(this.hdnActionID.Value), base.UrlEncode(this.hdnOwnerID.Value), ckb, base.Request.QueryString["OnLine"] }));
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            SkuQueryEntity skuQuery = this.GetSkuQueryEntity();
            if (int.Parse(skuQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                skuQuery.CurrentPage = (int.Parse(skuQuery.CurrentPage) + 1).ToString();
                this.BindData2SkuList(skuQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            SkuQueryEntity skuQuery = this.GetSkuQueryEntity();
            if (int.Parse(skuQuery.CurrentPage) > 0)
            {
                skuQuery.CurrentPage = (int.Parse(skuQuery.CurrentPage) - 1).ToString();
                this.BindData2SkuList(skuQuery);
            }
        }

        protected void btnSetActiveFalse_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> ownerID = base.GetSelectedIDsInGridView(this.gvSku, "OWNER_ID");
                List<string> skuID = base.GetSelectedIDsInGridView(this.gvSku, "SKU_ID");
                if ((skuID == null) || (skuID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2110_016") + BaseInfoDetailServiceClientProxy.GetInstance().UpdateIsActive(ownerID, skuID, "N", "SKU") + base.GetGlobalMsgResource("C018");
                    SkuQueryEntity skuQuery = this.GetSkuQueryEntity();
                    this.BindData2SkuList(skuQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnSetActiveTrue_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> ownerID = base.GetSelectedIDsInGridView(this.gvSku, "OWNER_ID");
                List<string> skuID = base.GetSelectedIDsInGridView(this.gvSku, "SKU_ID");
                if ((skuID == null) || (skuID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2110_016") + BaseInfoDetailServiceClientProxy.GetInstance().UpdateIsActive(ownerID, skuID, "Y", "SKU") + base.GetGlobalMsgResource("C017");
                    SkuQueryEntity skuQuery = this.GetSkuQueryEntity();
                    this.BindData2SkuList(skuQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        private SkuQueryEntity GetSkuQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as SkuQueryEntity);
            }
            return (base.PageQueryEntitySession as SkuQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                SkuQueryEntity skuQuery = this.GetSkuQueryEntity();
                skuQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2SkuList(skuQuery);
            }
        }

        protected void gvSku_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F2062", this.gvSku);
        }

        protected void gvSku_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
            }
        }

        protected void gvSku_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            if (string.IsNullOrEmpty(base.Request.QueryString["type"]))
            {
                this.hdnRtnValue.Value = base.UrlReplace(this.gvSku.DataKeys[e.NewSelectedIndex]["SKU_ID"].ToString());
            }
            else
            {
                this.hdnRtnValue.Value = base.UrlReplace(this.gvSku.DataKeys[e.NewSelectedIndex]["SKU_ID"].ToString()) + "@_@" + base.UrlReplace(this.gvSku.DataKeys[e.NewSelectedIndex]["OWNER_ID"].ToString()) + "@_@" + base.UrlReplace(this.gvSku.DataKeys[e.NewSelectedIndex]["DESCR"].ToString()) + "@_@" + base.UrlReplace(this.gvSku.DataKeys[e.NewSelectedIndex]["PACK_ID"].ToString());
            }
        }

        protected void gvSku_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                SkuQueryEntity skuQuery = this.GetSkuQueryEntity();
                skuQuery.SortField = e.SortExpression;
                if (skuQuery.SortDirection == "ASC")
                {
                    skuQuery.SortDirection = "DESC";
                }
                else if (skuQuery.SortDirection == "DESC")
                {
                    skuQuery.SortDirection = "ASC";
                }
                this.BindData2SkuList(skuQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ownerID"]))
            {
                this.hdnOwnerID.Value = base.Request.QueryString["ownerID"].ToString();
            }
            SkuQueryEntity skuQuery = new SkuQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    skuQuery.SkuID = this.PreviousPage.SkuID;
                    skuQuery.OwnerID = this.PreviousPage.OwnerID;
                    skuQuery.Descr = this.PreviousPage.SkuDescr;
                    skuQuery.IsActive = this.PreviousPage.IsActive;
                    skuQuery.PackID = this.PreviousPage.PackID;
                    skuQuery.LotTemplateID = this.PreviousPage.LotTemplateID;
                    skuQuery.SkuClassID = this.PreviousPage.SkuClassID;
                }
                else
                {
                    skuQuery.SkuID = "";
                    skuQuery.OwnerID = "";
                    skuQuery.Descr = "";
                    skuQuery.IsActive = "";
                    skuQuery.PackID = "";
                    skuQuery.LotTemplateID = "";
                    skuQuery.SkuClassID = "";
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
                {
                    skuQuery.OwnerID = this.PreviousPage.OwnerID;
                }
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            skuQuery.IsGetAll = false;
            skuQuery.CurrentPage = "0";
            skuQuery.PageSize = this.gvSku.PageSize.ToString();
            skuQuery.SortField = "SKU_ID";
            skuQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvSku.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                if (!string.IsNullOrEmpty(base.Request.QueryString["ckb"]))
                {
                    this.btnFinish.Visible = true;
                    this.gvSku.Columns[1].Visible = true;
                    this.gvSku.Columns[2].Visible = false;
                }
                if (string.IsNullOrEmpty(base.Request.QueryString["OnLine"]))
                {
                    this.btnFinish.Visible = false;
                    this.gvSku.Columns[1].Visible = false;
                    this.gvSku.Columns[2].Visible = true;
                }
                this.btnNew.Visible = false;
                this.btnEdit.Visible = false;
                this.btnDelete.Visible = false;
                this.btnAction.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetSkuQueryEntity(skuQuery);
            this.BindData2SkuList(skuQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetSkuQueryEntity(SkuQueryEntity skuQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = skuQuery;
            }
            else
            {
                base.PageQueryEntitySession = skuQuery;
            }
        }

        public SKUQuery PreviousPage
        {
            get
            {
                return (SKUQuery) base.PreviousPage;
            }
        }
    }
}

